package com.atguigu.spzx.cloud.user.controller;

import com.atguigu.spzx.cloud.user.entity.User;
import com.atguigu.spzx.cloud.user.properties.JavaProperties;
import com.atguigu.spzx.cloud.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

//@EnableConfigurationProperties(JavaProperties.class)
@RestController
@RequestMapping(value = "/api/user")
//  刷新配置
//@RefreshScope
public class UserController {


/*    @Value("${java.name:gusiyu}")
    private String name;

    @Value("${java.age:18}")
    private Integer age;*/
    @Autowired
    private UserService userService ;

/*    @Autowired
    private JavaProperties javaProperties ;*/

    // 优先级低的 [当前资源]
    @GetMapping(value = "/findUserByUserId/{userId}")
    public User findUserByUserId(@PathVariable(value = "userId") Long userId/*,
                                 @RequestHeader(value = "gusiyu", required = false)String gusiyu,
                                 @RequestHeader(value = "fuyachong", required = false)String fuyachong*/) throws InterruptedException {

        System.out.println("UserController.findUserByUserId" + System.currentTimeMillis());

        Random random = new Random();
        int i = random.nextInt(10);
        if (i < 5){
            throw  new RuntimeException("主动报错了"+ i);
        }

/*        System.out.println("UserController.findUserByUserId");
        System.out.println("userId = " + userId + ", gusiyu = " + gusiyu + ", fuyachong = " + fuyachong);
        System.out.println("name = " + name + ", age = " + age);
        System.out.println("=======================================================");
        System.out.println("javaProperties = " + javaProperties);*/

/*        System.out.println("进入");*//*
        try {
            Thread.sleep(2500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }*//*

//        int time =  new Random().nextInt(1000,3000);
//        System.out.println("time::"+time);
//        Thread.sleep(time);

        String name = null;
        name.toString();

        System.out.println("完毕");*/
        return userService.findUserByUserId(userId) ;
    }

    // TODO: 测试关联模式
    // 修改用户数据端点  [优先级高的！ 关联资源]
    @GetMapping(value = "/updateUserById")
    public String updateUserById() {
        return "修改用户数据成功";
    }


//======================================================================================
    @GetMapping(value = "/findUserByUserId/new/{userId}")
    public User findNewUserByUserId(@PathVariable(value = "userId") Long userId) {
        return userService.findNewUserByUserId(userId) ;
    }

//======================================================================================
// TODO: 测试链路模式
    @GetMapping(value = "/save")
    public String save() {
        userService.queryUsers();
        System.out.println("保存用户");
        return "订单保存成功" ;
    }

    @GetMapping(value = "/query")
    public String query() {
        userService.queryUsers();
        System.out.println("查询用户");
        return "查询用户成功" ;
    }

}